﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Models
{
    public class FollowupContract
    {
        public PageInfo Init()
        {
            PageInfo pg = new PageInfo();
            HttpSessionState session =System.Web.HttpContext.Current.Session;
            pg.dict = BasePage.getDict(session);
            pg.Type = "FollowupDeposit";
            pg.SMenuId = "KH";
            pg.TableName = "cu_Contract";
            pg.KeyFieldName = "id";
            pg.Sop = BasePage.Grant(session, pg.SMenuId);            
            pg.Sop.BROWSE = BasePage.Grant(session, pg.SMenuId, "GJ");
            pg.Sop.ADD = pg.Sop.EDIT = pg.Sop.DEL = false;
                       
            String id = "0";
            HttpRequest request = System.Web.HttpContext.Current.Request;
            if (request["Ids"] != null) id = Fun.cSql(request["Ids"]);

            String button = "";
            //退合同
            //button += "<a href =\"javascript: layer.confirm('您确定要退合同吗？', function (index) {layer.close(index);Do('../Customer/FollowupList/ContractReturn',{ id:[id],keyCode: GetKeyCode('[id]')},true);})\"  class='layui-btn layui-btn-xs layui-btn-danger' style='display:[btn1Show]'>退合同</a>";
            //退合同
            button += "<a href=\"javascript:ShowModalUrl('popEdit', '../Account/At?id=-1&FlowingType=00201001&cid=" + id + "&accountId=[accountId]&followupId=[id]&keyCode='+ GetKeyCode('[id]'), '退合同', '', '', '') \"  class='layui-btn layui-btn-xs layui-btn-danger' style='display:[btn1Show]'>退合同</a>";
            //延期
            button += "<a href=\"javascript:ShowModalUrl('popEdit', '../Customer/ContractEditDate?id=[id]&keyCode='+ GetKeyCode('[id]'), '延期', '', '360', '380') \"  class='layui-btn layui-btn-xs layui-btn-normal' style='display:[btn2Show]'>延期</a>";
           // ExeSql[1] = "update cu_Customers set CustState=3  where id=" + dt.Rows[0]["cid"].ToString();
           // ExeSql[2] = "update cu_Contract set status=0  where id=" + dt.Rows[0]["ContractId"].ToString();

            pg.Sql = String.Format(@"select t2.id,  
    t2.contractNumber,t1.ContractDate,t11.ContName as SetmealName,t2.Constart,t2.ConEnd,t2.CourseStart,t2.CourseEnd,
    ltrim(t2.DisPrice)+'/'+ltrim(t2.ContractPrice) ContractPrice,
    ltrim(t2.SurplusNumber)+'/'+ltrim(t2.ConCourseNumber+t2.GiveCourseNumber) as TotalCourseNumber,
    t9.DepositPrice,
    case when status in (2,3,4) then
			case 
				when t11.IsClassHour=1 then (SurplusNumber-GiveCourseNumber)*DisPrice/ConCourseNumber --按次算
				when DATEDIFF(dd,getdate(),courseEnd)+1>=0 then --按天数
						(DATEDIFF(dd,getdate(),courseEnd)+1)*DisPrice/DATEDIFF(dd,courseStart,courseEnd)
                else 0
			end
		else 0 end SurplusContractPrice,
    case when t2.status in (1,2,3,4) then 'inline-block' else 'none' end as btn1Show,  
    case when t2.status in (2,3,4) then 'inline-block' else 'none' end as btn2Show, 
	'' link
from 
cu_customers t1
inner join cu_Contract t2 on t1.id=t2.cid
left join (
        select ContractId,sum(account) DepositPrice  
        from cu_Account where state=3 and  flowingtype in('00101001','00201001') group by ContractId
) t9 on t1.id=t9.ContractId
left join cu_Contract_class t11 on t2.SetmealId=t11.id
where t1.custState=6 and t1.id={0}", id);
            ColumnInfo cl;
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "contractNumber"; cl.HeadText = "合同编号"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Date; cl.FieldName = "ContractDate"; cl.HeadText = "签订日期"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "SetmealName"; cl.HeadText = "合同套餐"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Date; cl.FieldName = "Constart"; cl.HeadText = "合同开始日期"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Date; cl.FieldName = "ConEnd"; cl.HeadText = "合同结束日期"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Date; cl.FieldName = "CourseStart"; cl.HeadText = "开课时间"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Date; cl.FieldName = "CourseEnd"; cl.HeadText = "开课结束时间"; pg.AddColumn(cl);

            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "ContractPrice"; cl.HeadText = "实收金额/合同金额"; cl.Style = "color:#FFB800"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "TotalCourseNumber"; cl.HeadText = "剩余课时/总课时"; cl.Style = "color:#FFB800"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Decimal; cl.FieldName = "DepositPrice"; cl.HeadText = "使用订金总额"; cl.Style = "color:#FFB800"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Decimal; cl.FieldName = "SurplusContractPrice"; cl.HeadText = "剩余合同金额"; cl.Style = "color:#FFB800"; pg.AddColumn(cl);

            //cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "AcNumber"; cl.HeadText = "收费编号"; pg.AddColumn(cl);
            //cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "Account"; cl.HeadText = "收费金额";  pg.AddColumn(cl);
            //cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "AccountModeCn"; cl.HeadText = "收费方式";  pg.AddColumn(cl);
            //cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "AccountRemark"; cl.HeadText = "收费备注";  pg.AddColumn(cl);
            //cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "AccountStateCn"; cl.HeadText = "入账状态"; pg.AddColumn(cl);
            cl = new ColumnInfo(); cl.DataType = Tag.Text; cl.FieldName = "link"; cl.HeadText = "操作"; cl.IsSort = false; cl.Script = button; pg.AddColumn(cl);
            return pg;
        }
    }
}